package com.bawei.a2010douyin.base.mvp;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bawei.a2010douyin.event.Event;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import androidx.annotation.IdRes;
import androidx.fragment.app.Fragment;


public abstract class BaseFragment extends Fragment implements BaseView {
    private  View view;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(bindLayout(), container, false);
        initView(savedInstanceState);
        initData();
        //注册广播
        EventBus.getDefault().register(this);
        return view;
    }
    //解除注册
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }
    //接受方法
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void receive(Event event){
        //判断是否为自己发送的广播
        if (event.target.equals(getClass().getSimpleName())){
            onEvent(event);
        }
    }
    //子类重写该方法就可以处理接受数据，不要在base里面做具体的处理
    public void onEvent(Event event){}

    public <T extends View> T findViewById(@IdRes int id) {
        return view.findViewById(id);
    }
    public abstract int bindLayout();
    public abstract void initView(Bundle savedInstanceState);
    public abstract void initData();

    @Override
    public void onOk() {

    }

    @Override
    public void onError() {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}